let get = require('../../utils/util')

Page({

  data: {
    num:1,
    url:getApp().globalData.domain,
    data:[],
    isWindowOpen:false,
    currentType:0,
    typeList:[],
    currentWeight:0,
    weightList:[],
    choosen:"",
    id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    get("shop/commodity/info",{id:options.id}).then(res=>{
      this.setData({
        data:res.data.data[0]
      })
    })
    get("shop/commodity/info/detail1",{id:options.id}).then(res=>{
      this.setData({
        typeList:res.data.data
      })
    })
    get("shop/commodity/info/detail2",{id:options.id}).then(res=>{
      this.setData({
        weightList:res.data.data
      })
    })


  },

  windowLisenter(event){
    this.setData({
      isWindowOpen:this.data.isWindowOpen?false:true
    })
 },

 numAdd(){
   this.setData({
     num:this.data.num = this.data.num+1
   })
 },
 numRed(){
  this.setData({
    num:this.data.num==1?1:this.data.num-1
  })
},
typeChange(event){
  this.setData({
    currentType:event.currentTarget.dataset.type_id
  })
},
weightChange(event){
  this.setData({
    currentWeight:event.currentTarget.dataset.type_id
  })
},
submit(){
  this.setData({
    choosen:this.data.typeList[this.data.currentType].name+this.data.weightList[this.data.currentWeight].weight+"斤"
  })
  this.setData({
    isWindowOpen:this.data.isWindowOpen?false:true
  })
},
fav(){
  get("shop/cart",{
    token:wx.getStorageSync('token'),
    name:this.data.typeList[this.data.currentType].name,
    weight:this.data.weightList[this.data.currentWeight].weight,
    bid:this.data.typeList[this.data.currentType].bid,
    number:this.data.num,
    img:this.data.typeList[this.data.currentType].img
  },'POST').then(res=>{
    if(res.data.data=="OK"){
      wx.showToast({
        title: '成功加入购物车',
      })
    }
  })
},
buy(event){
  wx.setStorage({
    data: [
      {
        name:this.data.typeList[this.data.currentType].name,
        number:this.data.num,
        weight:this.data.weightList[this.data.currentWeight].weight,
        bid:this.data.typeList[this.data.currentType].bid,
        img:this.data.typeList[this.data.currentType].img,

      }
    ],
    key: 'buy_info',
  })
  wx.navigateTo({
    url:"/shopping/pages/submit_order/submit_order"
  })

},
collect(){
  get("user/collect",{
    token:wx.getStorageSync('token'),
    commodity_id:this.data.id
  }).then(res=>{
    if(res.data.data=="OK"){
      wx.showToast({
        title: '收藏成功',
      })
    }
  })
}
  
})